package gurulab.maya.core.ws;

import electric.registry.Registry;
import electric.server.http.HTTP;
import gurulab.maya.core.MayaAntiFlood;
import gurulab.maya.data.DataManager;
import gurulab.merlin.core.ConfigurationCantHandException;
import gurulab.merlin.core.CantHandException;
import gurulab.merlin.core.PeerCantHandException;
import gurulab.merlin.core.envelope.AbstractEnvelopeProcessor;
import gurulab.merlin.core.envelope.Envelope;
import gurulab.merlin.server.EPContainer;
import gurulab.util.net.EMailer;

import java.io.FileInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class MayaEP extends AbstractEnvelopeProcessor {
    private Logger logger = Logger.getLogger (MayaEP.class);
    public static Properties properties;
    private DataManager datamanager;
    private MayaAntiFlood merAntiFlood;
    
    public MayaEP(EPContainer container,Properties props) {
		super(container,props);
	}
    
    public void openConnection (String parametersFilePath) throws CantHandException {
        try {
    //        FileInputStream is = new FileInputStream (parametersFilePath);
            setProperties (super.properties);
    //        getProperties ().load (is);
            String driver = getProperties ().getProperty ("db.driver");
            String dbmsURL = getProperties ().getProperty ("db.dbmsURL");
            String username = getProperties ().getProperty ("db.username");
            String password = getProperties ().getProperty ("db.password");
            
            this.setMerAntiFlood (new MayaAntiFlood (getProperties ()));
            this.setDataManager (new DataManager (driver, dbmsURL, username, password));
        } catch (Exception e) {
            throw new PeerCantHandException ("openConnection@MercurioEP" + e);
        }
    }
    
    public void start () {
        try {
            //String path = System.getProperty("path");
            String url_mercurio_wsdl = getProperties ().getProperty ("url_mercurio_wsdl", "http://mercurio.twindivision.com:8081/merlin/");
            HTTP.startup (url_mercurio_wsdl);
            //HTTP.startup(url_mercurio_wsdl + mercurio_wsdl);
            // System.setOut(realOut);
            Registry.publish("mercurio", new gurulab.maya.core.ws.Maya(this));
            logger.info ("Service is up and running at " + url_mercurio_wsdl);
        } catch (Exception e) {
            logger.error ("Error: " + e, e);
            stop ();
        }
        
    }
    
    public void stop () {
        logger.info ("Shuting down...");
        HTTP.shutdown ();
        logger.info ("done!");
    }
    
    /*
     * (non-Javadoc)
     *
     * @see gurulab.merlin.core.AbstractEnvelopeProcessor#handEnvelope(gurulab.merlin.core.Envelope)
     */
    public Object handEnvelope (Envelope envelope) throws CantHandException {
        throw new ConfigurationCantHandException (envelope.toString ());
    }
    
    /* (sin Javadoc)
     * @see gurulab.merlin.core.EnvelopeHandler#retrieveEnvelope(java.lang.Object)
     */
    public void retrieveEnvelope (Object EnvelopeReference) throws CantHandException {
    }
    
    /* (sin Javadoc)
     * @see gurulab.merlin.core.EnvelopeHandler#commitHandEnvelope(java.lang.Object)
     */
    public void commitHandEnvelope (Object EnvelopeReference) throws CantHandException {
    }
    
    
    public Properties getProperties () {
        return MayaEP.properties;
    }
    
    public void setProperties (Properties properties) {
        MayaEP.properties = properties;
    }
    
    public MayaAntiFlood getMerAntiFlood () {
        return merAntiFlood;
    }
    
    public void setMerAntiFlood (MayaAntiFlood merAntiFlood) {
        this.merAntiFlood = merAntiFlood;
    }
    
    public DataManager getDataManager () {
        return this.datamanager;
    }
    
    public void setDataManager (DataManager dm) {
        this.datamanager = dm;
    }
    
    public boolean estaOcupado() {
    	// TODO Auto-generated method stub
    	return false;
    }
}
